<template>
  <div class="mod-config">
    <el-button-group style="margin-bottom: 10px;">
      <el-button type="primary" icon="el-icon-search" @click="searchHandle">查找</el-button>
      <el-button type="primary" icon="el-icon-delete" @click="searchReset">清空</el-button>
      <el-button v-if="permissions.eims_integralrule_add" icon="el-icon-plus" type="primary" @click="addOrUpdateHandle()">新增</el-button>
      <el-button v-if="permissions.eims_integralrule_batchdel" icon="el-icon-plus" type="primary" @click="batchDel" :loading="isDel">批量删除</el-button>
      <el-button v-if="permissions.eims_integralrule_batchedit" icon="el-icon-plus" type="primary" @click="batchEdit" :loading="isEdit">批量修改</el-button>
    </el-button-group>
    <el-form inline label-width="100px" @keyup.enter.native="searchHandle">
      <el-form-item label="商品类别" prop="ctgId">
        <el-select v-model="dataForm.ctgId" @change="getSupplyList">
          <el-option v-for="item in ctgIdOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="制造商" prop="supplyId">
        <el-select v-model="dataForm.supplyId" @change="getBrandList">
          <el-option v-for="item in supplyOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="商品品种" prop="brandId">
        <el-select v-model="dataForm.brandId">
          <el-option v-for="item in brandOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="商品代码" prop="goodsCode">
        <el-input v-model="dataForm.goodsCode"></el-input>
      </el-form-item>
      <el-form-item label="商品名称" prop="goodsName">
        <el-input v-model="dataForm.goodsName"></el-input>
      </el-form-item>
      <el-form-item label="部门" prop="deptId">
        <el-select v-model="dataForm.deptId">
          <el-option v-for="item in deptOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="卡类型" prop="cardType">
        <el-select v-model="dataForm.cardType">
          <el-option v-for="item in cardTypeOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div class="avue-crud">
      <el-table :data="dataList" border v-loading="dataListLoading" :max-height="tableHeight" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column prop="ctgName" label="商品类别" width="80"></el-table-column>
        <el-table-column prop="supplyName" label="制造商" width="80"></el-table-column>
        <el-table-column prop="brandName" label="商品品种" width="80"></el-table-column>
        <el-table-column prop="goodsCode" label="商品编码" width="80"></el-table-column>
        <el-table-column prop="goodsName" label="名称" width="80"></el-table-column>
        <el-table-column prop="deptNames" label="活动门店" min-width="320"></el-table-column>
        <el-table-column prop="exchangeRate" label="1元兑换积分比例" width="120"></el-table-column>
        <el-table-column prop="cardType" label="卡类型" :formatter="formatter" width="120"></el-table-column>
        <el-table-column label="操作" fixed="right" width="140">
          <template slot-scope="scope">
            <el-button v-if="permissions.eims_integralrule_edit" type="text" icon="el-icon-edit" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
            <el-button v-if="permissions.eims_integralrule_del" type="text" icon="el-icon-delete" @click="deleteHandle(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div class="avue-crud__pagination">
      <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
        :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage" background layout="total, sizes, prev, pager, next"></el-pagination>
    </div>
    <table-form v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></table-form>
    <table-form2 v-if="batchEditVisible" ref="batchEdit" @refreshDataList="getEditData"></table-form2>
  </div>
</template>

<script>
  import { mapGetters } from "vuex"
  import { filterParams } from "@/util/tools"
  import { fetchCtgSaleList } from "@/api/eims/ctg"
  import { fetchSupplyList } from '@/api/eims/supply'
  import { fetchBrandList } from '@/api/eims/brand'
  import { goodsFetchList } from '@/api/eims/goods'
  import { getDeptInfoByDeptType } from '@/api/admin/dept'
  import {
    fetchList,
    delObj,
    batchDelInfo
  } from "@/api/eims/integralrule"

  import TableForm from "./integralrule-form"
  import TableForm2 from "./batch-form"

  export default {
    data() {
      return {
        isDel: false,
        isEdit: false,
        dataForm: {
          ctgId: "",
          supplyId: "",
          brandId: "",
          goodsCode: "",
          goodsName: "",
          deptId: "",
          cardType: ""
        },
        dataList: [],
        ctgIdOptions: [],
        supplyOptions: [],
        brandOptions: [],
        deptOptions: [],
        cardTypeOptions: [{
          value: '0',
          label: '非会员卡'
        }, {
          value: '1',
          label: '会员卡'
        }, {
          value: '2',
          label: '所有类型卡'
        }],
        searchShow: true,
        pageIndex: 1,
        pageSize: 100,
        totalPage: 0,
        dataListLoading: false,
        addOrUpdateVisible: false,
        batchEditVisible: false,
        ids: []
      };
    },
    components: {
      TableForm,
      TableForm2
    },
    created() {
      this.getDataList();
      this.getCtgList();
      this.getDeptList();
    },
    computed: {
      ...mapGetters(["permissions"]),
      tableHeight: function() {
        return window.innerHeight - 260
      }
    },
    methods: {
      //	查询数据
      searchHandle() {
        this.pageIndex = 1
        this.getDataList()
      },
      // 获取数据列表
      getDataList() {
        let params1 = {
          current: this.pageIndex,
          size: this.pageSize
        }
        let params2 = filterParams(this.dataForm)
        this.dataListLoading = true;
        fetchList(Object.assign(params1, params2)).then(response => {
          if(response.data.code == '0'){
            this.dataList = response.data.data.records
            this.totalPage = response.data.data.total
          }else{
            this.$message.error(response.data.msg)
          }
          this.dataListLoading = false
        });

      },
      // 每页数
      sizeChangeHandle(val) {
        this.pageSize = val;
        this.pageIndex = 1;
        this.getDataList();
      },
      // 当前页
      currentChangeHandle(val) {
        this.pageIndex = val;
        this.getDataList();
      },
      // 新增 / 修改
      addOrUpdateHandle(id) {
        this.addOrUpdateVisible = true;
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id);
        });
      },
      // 删除
      deleteHandle(id) {
        this.$confirm("是否确认删除ID为" + id, "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          })
          .then(function() {
            return delObj(id);
          })
          .then(response => {
            if(response.data.code == '0'){
              this.$message.success("删除成功")
              this.getDataList()
            }else{
              this.$message.error(response.data.msg)
            }
          })
      },
      //批量删除
      batchDel() {
        this.isDel = true
        if (!this.ids.length) {
          this.isDel = false
          this.$message.warning('请选择删除对象')
          return
        }
        batchDelInfo(this.ids).then(response => {
          if(response.data.code == '0'){
            this.isDel = false
            this.$message.success("批量删除成功")
            this.getDataList()
          }else{
            this.$message.error(response.data.msg)
          }
        }).catch(() => {
          this.isDel = false
        })
      },
      // 批量修改
      batchEdit() {
        this.isEdit = true
        if (!this.ids.length) {
          this.isEdit = false
          this.$message.warning('请选择修改对象')
          return
        }
        this.batchEditVisible = true;
        this.$nextTick(() => {
          this.$refs.batchEdit.init(this.ids);
        });
      },
      getEditData(data){
        this.isEdit = false
        this.getDataList()
      },
      searchChange() {
        let params1 = filterParams(this.dataForm);
        let params2 = {
          current: this.pageIndex,
          size: this.pageSize
        };
        fetchList(Object.assign(params2, params1)).then(response => {
          if(response.data.code == '0'){
            this.dataList = response.data.data.records
            this.totalPage = response.data.data.total
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
      // 搜索清空
      searchReset() {
        this.supplyOptions = []
        this.brandOptions = []
        this.$refs.searchForm.resetFields();
      },
      handleSearchShow() {
        this.searchShow = !this.searchShow;
      },
      /**
       * 刷新回调
       */
      refreshChange() {
        this.getDataList(this.page);
      },
      formatter(row, column, cellValue, index) {
        switch (cellValue) {
          case 0:
            return '非会员卡'
            break
          case 1:
            return '会员卡'
            break
          case 2:
            return '非会员卡,会员卡'
            break
        }
      },
      getCtgList() {
        fetchCtgSaleList({
          type: 3
        }).then(response => {
          if(response.data.code == '0'){
            let result = response.data.data
            let list = []
            result.forEach(item => {
              list.push({
                value: item.id,
                label: item.name
              })
            })
            this.ctgIdOptions = list
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
      getSupplyList(val) {
        this.dataForm.supplyId = ''
        this.dataForm.brandId = ''
        let params = {
          current: 1,
          size: 100000,
          ctgId: val
        }
        fetchSupplyList(params).then(response => {
          if(response.data.code == '0'){
            let resultData = response.data.data.records
            let list = []
            resultData.forEach(item => {
              list.push({
                value: item.id,
                label: item.name
              })
            })
            this.supplyOptions = list
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
      getBrandList(val) {
        this.dataForm.brandId = ''
        let params = {
          current: 1,
          size: 100000,
          supplyId: val
        }
        fetchBrandList(params).then(response => {
          if(response.data.code == '0'){
            let resultData = response.data.data.records
            let list = []
            resultData.forEach(item => {
              list.push({
                value: item.id,
                label: item.name
              })
            })
            this.brandOptions = list
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
      getDeptList() {
        getDeptInfoByDeptType(1).then(response => {
          if(response.data.code == '0'){
            let result = response.data.data
            let list = []
            result.forEach(item => {
              list.push({
                value: item.id,
                label: item.name
              })
            })
            this.deptOptions = list
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
      // 选择事件 得到选中的数据
      handleSelectionChange(data) {
        let list = []
        this.tableDataAmount = data
        data.forEach(item => {
          list.push(item.id)
        })
        this.ids = list
      },
    }
  };
</script>
